<?php
namespace App\Services;
use App\Models\TagModel;

class TagService{
	/**
	 * 获取标签总数
	 * @return int $tNum
	 */
	public function getTagCount(){
		$tNum = TagModel::where('isshow',1)->count();
		return $tNum;
	}

	/**
	 * 获取标签列表
	 * @param int $pagesize
	 * @param string $keywords
	 * @return object $taglist
	 */
	public function getTagList($pagesize,$keywords=''){
		$taglist = TagModel::orderBy('id','asc')
					->where('isshow',1)
					->where(function($query) use ($keywords){
						if($keywords != ''){
							$query->where('tag_name', 'like', '%'.$keywords.'%');
						}
					})
					->select('id','tid','tag_name','add_date')
					->paginate($pagesize);
		
		return $taglist;
	}

	/**
	 * 获取标签详细信息
	 * @param string $name
	 * @return object $info
	 */
	public function getTagInfoByName($name){
		$info = TagModel::where('isshow',1)->where('tag_name',$name)->first();
		return $info;
	}

	/**
	 * 获取标签详细信息
	 * @param int $id
	 * @return object $info
	 */
	public function getTagInfoById($id){
		$info = TagModel::where('id',$id)->first();
		return $info;
	}

	/**
	 * 添加标签
	 * @param array $data
	 * @return $res
	 */
	public function insertTag($data){
		$res = TagModel::insert($data);
		return $res;
	}

	/**
	 * 更新标签
	 * @param int $id
	 * @param array $data
	 * @return $res
	 */
	public function modifyTag($id,$data){
		$res = TagModel::where('id',$id)->update($data);
		return $res;
	}
}